package AssertAll;

import android.sax.RootElement;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;

import Elemenets.HomeElements;
import util.GetDevice;
import util.UIHelper;

/**
 * Created by zhe.xiao on 2017/7/28.
 */

public class AssertHome {
    private static AssertHome assertHome;
    private GuessYouGoAssert guessYouGoAssert;
    private UiDevice device;
    private RecomendTimeAssert recomendTimeAssert;
    private AssertHome(){}

    public GuessYouGoAssert getGuessYouGoAssert() {
        if(guessYouGoAssert == null){
            guessYouGoAssert = GuessYouGoAssert.getInstent(device);
        }
        return guessYouGoAssert;
    }

    public RecomendTimeAssert getRecomendTimeAssert(){
        if(recomendTimeAssert == null){
            recomendTimeAssert = RecomendTimeAssert.getInstent(device);
        }
        return recomendTimeAssert;
    }

    public UiDevice getDevice(){
        if(device == null){
            device = GetDevice.getDevice();
        }
        return device;
    }

    public static AssertHome getAssertHome(){
        if(assertHome == null){
            assertHome = new AssertHome();
            assertHome.getDevice();
        }
        return assertHome;
    }

    public static class GuessYouGoAssert{
        private static GuessYouGoAssert guessYouGoAssert;
        private UiDevice device;
        private GuessYouGoAssert(UiDevice device){
            this.device = device;
        }
        public static GuessYouGoAssert getInstent(UiDevice device){
            if(guessYouGoAssert == null){
                guessYouGoAssert = new GuessYouGoAssert(device);
            }
            return guessYouGoAssert;
        }

        /**
         * 景点页面的tile
         * @return
         */
        public String getTickedTitle(){
            UiObject2 u2 = device.wait(Until.findObject(By.res("com.mqunar.atom.voice:id/atom_voice_title_search_edittext")), 20000);
            String title = u2.getText();
            return title.trim();
        }

        /**
         * 获取景点list页的展示的景点的个数
         * @return
         */
        public int getTicketsNum(){
            UIHelper.getElementsAction().sleep(2000);
            UiObject2 u2 = device.wait(Until.findObject(By.clazz("android.widget.ListView")), 20000);
            int i = u2.getChildren().size();
            return i;
        }

        /**
         * 获取酒店list页的地标名称
         * @return
         */
        public String getHotleName(){
            UiObject2 u2 = device.wait(Until.findObject(By.res("com.mqunar.atom.hotel:id/atom_hotel_et_suggest")), 20000);
            String name = u2.getText().trim();
            return name;
        }

        /**
         * 获取酒店list页面的酒店个数
         * @return
         */
        public int getHotleNum(){
            UIHelper.getElementsAction().sleep(2000);
            UiObject2 u2 = device.wait(Until.findObject(By.res("android:id/list")), 20000);
            int num = u2.getChildren().size();
            return num;
        }

        /**
         * 获取度假页面的筛选
         * @return
         */
        public String getVacationSearch(){
            UiObject2 u2 = device.wait(Until.findObject(By.desc("筛选")), 20000);
            String s = u2.getContentDescription();
            return s;
        }

        /**
         * 获取度假页面的最近关注
         * @return
         */
        public String getVacationZui(){
            UiObject2 u2 = device.wait(Until.findObject(By.desc("最近关注")), 20000);
            String s = u2.getContentDescription();
            return s;
        }

        /**
         * 获取度假list的单品数量
         * @return
         */
        public int getVacationNum(){
            UIHelper.getElementsAction().sleep(2000);
            UiObject2 u2 = device.wait(Until.findObject(By.clazz("android.widget.ListView")), 20000);
            int i = u2.getChildren().size();
            return i;
        }

        /**
         * 获取智能中转的tag数量
         * @return
         */
        public int getZhiNengZhongZhuan(){
            UIHelper.getElementsAction().sleep(2000);
            UiObject2 u2 = device.wait(Until.findObject(By.clazz("android.widget.ListView")), 20000);
            int i = u2.getChildren().size();
            return i;
        }
    }

    public static class RecomendTimeAssert{
        private static RecomendTimeAssert recomendTimeAssert;
        private UiDevice device;

        private RecomendTimeAssert(UiDevice device){
            this.device = device;
        }

        public static RecomendTimeAssert getInstent(UiDevice device){
            if(recomendTimeAssert == null){
                recomendTimeAssert = new RecomendTimeAssert(device);
            }
            return recomendTimeAssert;
        }

        /**
         * 门票类型的验证点
         * @return
         */
        public String getTicketName(){
            UIHelper.getElementsAction().sleep(5000);
            UiObject2 u = device.wait(Until.findObject(By.desc("搜索")), 20000);
            return u.getChildren().get(0).getChildren().get(1).getChildren().get(1).getContentDescription().trim();
        }

        /**
         * 飞机类型验证点
         * @return
         */
        public String getFlay(){
            UiObject2 u = device.wait(Until.findObject(By.textStartsWith("经济舱")), 20000);
            return u.getText().trim();
        }

        /**
         * 酒店类型验证点
         * @return
         */
        public String getHotel(){
            UiObject2 u = device.findObject(By.res("com.mqunar.atom.hotel:id/atom_hotel_tx_title"));
            String t = u.getText();
            return t;
        }
    }
}
